﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows.Forms;

namespace CertificateVerificationSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 选择证书文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = openFileDialog1.ShowDialog();
            if(dialogResult != DialogResult.OK|| string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                return;
            }
            var file = openFileDialog1.FileName;
            if(System.IO.File.Exists(file) == false)
            {
                MessageBox.Show("文件不存在");
                return;
            }
            this.txtFilePath.Text = file;
        }
        // 设置保存文件位置 
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = saveFileDialog1.ShowDialog();
            if (dialogResult != DialogResult.OK || string.IsNullOrEmpty(saveFileDialog1.FileName))
            {
                return;
            }
            var file = saveFileDialog1.FileName;
            this.txtSavePath.Text = file;
        }
        //验证
        private void button1_Click_1(object sender, EventArgs e)
        {
            var file = this.txtFilePath.Text;
            if (System.IO.File.Exists(file) == false)
            {
                MessageBox.Show("文件不存在");
                return;
            }
            if (string.IsNullOrEmpty(this.txtCode.Text))
            {
                MessageBox.Show("请输入验证码");
                return;
            }
            if (string.IsNullOrEmpty(this.txtSavePath.Text))
            {
                MessageBox.Show("请设置验证文件保存位置");
                return;
            }
            var certificatePath = file.Trim();
            var certificatePassword = this.txtPassword.Text;
            var code = this.txtCode.Text;
            var savePath = this.txtSavePath.Text;
            var algorithm = Convert.ToString(this.cbmAlgorithm.SelectedItem);
            var keyLength = Convert.ToInt32(this.cmbKeyLength.SelectedItem);
            Verification(certificatePath, certificatePassword, code, savePath, algorithm, keyLength);
        }


        private void Verification(string certificatePath, string certificatePassword, string verificationCode,string savePath,
            string algorithm, int keyLength)
        {
            var certificate = new X509Certificate2(
                certificatePath,
                certificatePassword,
                X509KeyStorageFlags.Exportable);
            if (!certificate.HasPrivateKey)
            {
                MessageBox.Show("ERROR: The certificate does not have a private key.");
                return ;
            }
            X509Certificate2 verificationCertificate =
                VerificationCertificateGenerator.GenerateSignedCertificate(certificate, verificationCode, algorithm,keyLength);

            File.WriteAllText(
                savePath,
                Convert.ToBase64String(verificationCertificate.Export(X509ContentType.Cert)));

            MessageBox.Show(
                $"Verification certificate ({verificationCertificate.Subject}; {verificationCertificate.Thumbprint})" +
                $" was written to {savePath}.");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.cbmAlgorithm.SelectedIndex = 0;
            this.cmbKeyLength.SelectedIndex = 3;
        }
    }
}
